package com.apexsoft.assets.base.utils;

import com.apexsoft.aas.service.model.AUploadResponse;
;
import com.apexsoft.assets.base.PagedArrayList;
import com.apexsoft.assets.base.response.AmsResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.cglib.beans.BeanMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guojieming1
 * @date 2021-1-26
 */
public class AResultUtil {
    public static final int CODE_SUCCESS = 1;
    public static final int CODE_ERROR = -1;

    public static <T> AmsResponse<T> success() {
        AmsResponse<T> response = new AmsResponse<>();
        response.setCode(CODE_SUCCESS);
        return response;
    }

    public static <T> AmsResponse<T> success(List<T> data) {
        AmsResponse<T> response = success();
        if(data!=null /*&& !data.isEmpty()*/){
            List<Map<String, Object>> result = new ArrayList<>();
            for(T t:data){
                if(t instanceof Map){
                    result.add((Map<String, Object>) t);
                }else {
                    result.add(BeanMap.create(t));
                }
            }
            response.setRecords(result);
            if(data instanceof PagedArrayList && ((PagedArrayList<T>) data).getTotal()>data.size()){
                response.setTotalRecord((long) ((PagedArrayList<T>) data).getTotal());
            }else{
                response.setTotalRecord((long) data.size());
            }
        }
        return response;
    }

    public static <T> AmsResponse<T> success(List<T> data, int currentPage, int pageSize){
        AmsResponse<T> response = success(data);
        response.setPageSize(pageSize);
        response.setCurrentPage(currentPage);
        response.setTotalPage(calculateTotalPage(pageSize, response.getTotalRecord()));
        return response;
    }

    public static AUploadResponse uploadSuccess(Map<String,Object> data){
        AUploadResponse response = new AUploadResponse();
        response.setCode(1);
        response.setNote("success");
        response.setData(data);
        return response;
    }

    public static AUploadResponse uploadSuccess(String note, Map<String,Object> data){
        AUploadResponse response = new AUploadResponse();
        response.setCode(1);
        response.setNote(note);
        response.setData(data);
        return response;
    }
    public static AUploadResponse uploadError(String note){
        AUploadResponse response = new AUploadResponse();
        response.setCode(-1);
        response.setNote(note);
        return response;
    }

    public static <T> AmsResponse<T> success(Page<T> data){
        AmsResponse<T> response = success(data.getRecords());
        response.setPageSize(data.getSize());
        response.setCurrentPage(data.getCurrent());
        response.setTotalPage(data.getPages());
        response.setTotalRecord(data.getTotal());
        return response;
    }
    /**
     * linzk  重写响应方法
     *  data：符合的集合
     *  totalRecord：总记录数
     */
    public static <T> AmsResponse<T> successResponse(List<T> data,Long totalRecord) {
        AmsResponse<T> response = success();
        if(data!=null /*&& !data.isEmpty()*/){
            List<Map<String, Object>> result = new ArrayList<>();
            for(T t:data){
                if(t instanceof Map){
                    result.add((Map<String, Object>) t);
                }else {
                    result.add(BeanMap.create(t));
                }
            }
            response.setRecords(result);
            response.setTotalRecord(totalRecord);


        }
        return response;
    }

    public static long calculateTotalPage(long pageSize, long totalRecord){
        if(pageSize<1) {
            return -1;
        }
        if(totalRecord<0) {
            return -1;
        }
        if(totalRecord==0) {
            return 0;
        }
        return totalRecord/pageSize + (totalRecord%pageSize==0?0:1);
    }

    @Deprecated
    public static <T> AmsResponse<T> successRecords(List<T> list) {
        AmsResponse<T> response = success();
        if(list!=null /*&& !list.isEmpty()*/){
            List<Map<String, Object>> result = new ArrayList<>();
            for(T t:list){
                result.add(BeanMap.create(t));
            }
            response.setRecords(result);
        }
        return response;
    }

    public static <T> AmsResponse<T> success(Map<String, Object> data){
        AmsResponse<T> response = success();
        if(data!=null && !data.isEmpty()){
            response.setData(data);
        }
        return response;
    }

    @Deprecated
    public static <T> AmsResponse<T> successData(Map<String, Object> data) {
        AmsResponse<T> response = success();
        if(data!=null && !data.isEmpty()){
            response.setData(data);
        }
        return response;
    }

    public static AUploadResponse successUploadData(Map<String, Object> data) {
        AUploadResponse response = new AUploadResponse();
        response.setCode(CODE_SUCCESS);
        if(data!=null && !data.isEmpty()){
            response.setData(data);
        }
        return response;
    }

    public static <T> AmsResponse<T> successData(T data) {
        AmsResponse<T> response = success();
        if(data!=null){
            response.setData(BeanMap.create(data));
        }else{
            response.setData(new HashMap<>());
        }
        return response;
    }

    public static <T> AmsResponse<T> successNote(String note) {
        AmsResponse<T> response = success();
        response.setNote(note);
        return response;
    }

    public static <T> AmsResponse<T> result(int code, String note) {
        AmsResponse<T> response = new AmsResponse<>();
        response.setCode(code);
        response.setNote(note);
        return response;
    }

    public static <T> AmsResponse<T> error(String note) {
        return result(CODE_ERROR, note);
    }
}
